package com.zhangtai.utils;


import com.zhangtai.exception.RException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;

/**
 * @Classname EmailUtil
 * @Description 邮件工具类
 * @Author Created by Lihaodong (alias:小东啊) lihaodongmail@163.com
 * @Date 2019-06-15 10:47
 * @Version 1.0
 */
@Component
@Slf4j
public class EmailUtil {

    /**
     * 获取JavaMailSender bean
     */
    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * 获取配置文件的username
     */
    @Value("${spring.mail.username}")
    private String username;


    /**
     * 带附件
     * @param file
     * @param fileName
     * @param to
     * @param subject
     * @param content
     * @throws MessagingException
     */
    public void sendMail(MultipartFile file,String fileName,String to,String subject, String content ) throws MessagingException {

        log.info("开始发送邮件:{}",to);

        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.addAttachment(fileName,file);
        helper.setFrom(username);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content);

        javaMailSender.send(mimeMessage);
        log.info("{}发送成功",to);
//
    }


    public void sendSimpleMail(String to,String subject,String content) {
        log.info("开始发送邮件:{}",to);
        SimpleMailMessage message = new SimpleMailMessage();
        //设定邮件参数
        //发送者
        message.setFrom(username);
        //接收者
        message.setTo(to);
        //主题
        message.setSubject(subject);
        //邮件内容
        message.setText(content);
        // 发送邮件
        javaMailSender.send(message);

        log.info("发送成功:{}",to);
    }

    public void sendHtml(String to,String subject,String content) throws MessagingException {
        log.info("开始发送邮件:{}",to);
        MimeMessage message=javaMailSender.createMimeMessage();
        MimeMessageHelper helper=new MimeMessageHelper(message,true);
        //设定邮件参数
        helper.setFrom(username);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content,true);

        // 发送邮件
        javaMailSender.send(message);

        log.info("发送html成功:{}",to);
    }

    public void sendHtmlAttachment(String to, String subject, String content, File file,String fileName) throws MessagingException {
        log.info("开始发送邮件:{}",to);
        MimeMessage message=javaMailSender.createMimeMessage();
        MimeMessageHelper helper=new MimeMessageHelper(message,true);
        //设定邮件参数
        helper.addAttachment(fileName,file);
        helper.setFrom(username);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content,true);

        // 发送邮件
        javaMailSender.send(message);

        log.info("发送html成功:{}",to);
    }

    public void sendHtmlAttachmentList(String to, String subject, String content, List<String> urls) throws Exception {




        log.info("开始发送邮件:{}",to);
        MimeMessage message=javaMailSender.createMimeMessage();
        MimeMessageHelper helper=new MimeMessageHelper(message,true);
        //设定邮件参数
        for (String url : urls) {
            File file = IoUtils.getFileByUrl(url);
            String[] split = url.split("----");
            if (split==null||split.length<=1){
                throw new RException("阿里云url后缀文件名需要以----分割");

            }
            helper.addAttachment(split[1],file);
        }

        helper.setFrom(username);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content,true);

        // 发送邮件
        javaMailSender.send(message);

        log.info("发送html成功:{}",to);
    }



}
